package com.wujian.love.controller;

import com.wujian.love.redis.RedisKey;
import com.wujian.love.redis.RedisService;
import com.wujian.love.redis.stream.MessageStreamProducerService;
import com.wujian.love.utils.CodeGeneratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.StreamEntryID;

import java.util.HashMap;

/**
 * @ClassName: RedisStreamController
 * @Description: stream 数据操作
 * @Author: wuj
 * @Date: 2021-01-28 16:13
 **/
@RestController
@RequestMapping("/stream")
public class RedisStreamController {


    @Autowired
    private MessageStreamProducerService producerService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisService redisService;


    @GetMapping("/redisOrder")
    public void redisOrderNo(Long userId) throws InterruptedException {
        String orderNumber = CodeGeneratorUtils.nextId();
        String redisKey = RedisKey.getOrderNumber(userId);
        HashMap<String,String> map = new HashMap<>();
        map.put("orderNumber",orderNumber);
        //添加消息 发送订单消息
        producerService.sendMsg(map, redisKey);
        //创建消费组
        redisTemplate.convertAndSend("order-stream",redisKey);

    }

    @GetMapping("/getKey")
    public void getKey(Long userId) throws InterruptedException {
        //String redisKey = RedisKey.getOrderNumber(userId);
        String redisKey = "redisKey";
        redisService.xGetKey(redisKey);

    }
    @GetMapping("/groupRead")
    public void groupRead() throws InterruptedException {
        redisService.groupRead();

    }


}
